<?php
/**
 * 导出管理
*/
apf_require_class("Bll_BLL");
class Bll_Hr_Export extends BLL{

    /**
     * 入职用户数据到出到excel
     * @param array $data
     * @param int $start
     * @param int $end
     */
    public function build_enter_user($data,$start,$end){
        apf_require_class('PhpExcelHelper');

        if(empty($data)) {
            return '';
        }
        $filename = '入职员工_'.$start.'-'.$end.'.xls';
        $title_width = array(
         array('width'=>40,'name'=>'组织单元名称'),
         array('width'=>15,'name'=>'职位'),
         array('width'=>10,'name'=>'姓名'),
         array('width'=>5,'name'=>'代码'),
         array('width'=>20,'name'=>'身份证号码'),
         array('width'=>20,'name'=>'护照号码'),
         array('width'=>10,'name'=>'主身份标识'),
         array('width'=>20,'name'=>'用户名'),
         array('width'=>5,'name'=>'性别'),
         array('width'=>10,'name'=>'员工状态'),
         array('width'=>10,'name'=>'员工类别'),
         array('width'=>10,'name'=>'入司日期'),
         array('width'=>15,'name'=>'担任本职位日期'),
         array('width'=>8,'name'=>'试用期'),
         array('width'=>10,'name'=>'事务日期'),
         array('width'=>10,'name'=>'事务类型'),
         array('width'=>30,'name'=>'Email地址'),
         array('width'=>10,'name'=>'入职类型'),
         array('width'=>10,'name'=>'职员代码'),
         array('width'=>10,'name'=>'出生日期'),
         array('width'=>5,'name'=>'民族'),
         array('width'=>10,'name'=>'政治面貌'),
         array('width'=>10,'name'=>'参加工作时间'),
         array('width'=>10,'name'=>'出生地'),
         array('width'=>10,'name'=>'个人成份'),
         array('width'=>10,'name'=>'工资代码'),
         array('width'=>10,'name'=>'婚姻状况'),
         array('width'=>10,'name'=>'籍贯'),
         array('width'=>10,'name'=>'家庭出身'),
         array('width'=>10,'name'=>'健康状况'),
         array('width'=>10,'name'=>'离退休时间'),
         array('width'=>45,'name'=>'联系地址'),
         array('width'=>20,'name'=>'联系电话'),
         array('width'=>15,'name'=>'入司前连续工作时间'),
         array('width'=>10,'name'=>'社保账号'),
         array('width'=>45,'name'=>'身份证地址'),
         array('width'=>5,'name'=>'身高'),
         array('width'=>20,'name'=>'手机号码'),
         array('width'=>5,'name'=>'体重'),
         array('width'=>10,'name'=>'现从事专业'),
         array('width'=>5,'name'=>'血型'),
         array('width'=>10,'name'=>'银行名称'),
         array('width'=>30,'name'=>'银行账号'),
         array('width'=>10,'name'=>'邮政编码'),
         array('width'=>5,'name'=>'曾用名'),
         array('width'=>15,'name'=>'SMTP服务认证账号'),
         array('width'=>15,'name'=>'SMTP服务认证密码'),
         array('width'=>15,'name'=>'不在职日期'),
         array('width'=>15,'name'=>'成本中心'),
         array('width'=>15,'name'=>'工作所在地'),
         array('width'=>10,'name'=>'雇佣类别'),
         array('width'=>15,'name'=>'所属其他组织'),
         array('width'=>45,'name'=>'现居住地址'),
         array('width'=>45,'name'=>'户口所在地'),
         array('width'=>20,'name'=>'人事档案所在地'),
         array('width'=>10,'name'=>'户粮性质'),
         array('width'=>15,'name'=>'养老保险类型'),
         array('width'=>5,'name'=>'专长'),
         array('width'=>10,'name'=>'兴趣爱好'),
         array('width'=>20,'name'=>'公司电话'),
         array('width'=>20,'name'=>'公司内部职位'),
         array('width'=>10,'name'=>'英文名'),
         array('width'=>5,'name'=>'昵称'),
         array('width'=>20,'name'=>'加入本部门日期'),
         array('width'=>20,'name'=>'加入本集团日期'),
         array('width'=>15,'name'=>'薪酬标准分类'),
         array('width'=>20,'name'=>'再次入集团日期'),
         array('width'=>15,'name'=>'同步工资生效日期'),
         array('width'=>20,'name'=>'转正日期'),
         array('width'=>5,'name'=>'职级'),
         array('width'=>15,'name'=>'录用组织单元ID'),
         array('width'=>20,'name'=>'录用组织单元名称'),
         array('width'=>20,'name'=>'担任当前职级日期'),
         array('width'=>20,'name'=>'开始担任当前职务日期'),
         array('width'=>20,'name'=>'是否为后备人才'),
         array('width'=>20,'name'=>'是否处于试用考察期'),
         array('width'=>20,'name'=>'试用考察期开始日期'),
         array('width'=>20,'name'=>'试用考察期转正日期'),
         array('width'=>20,'name'=>'是否有修改内部异动人事事务中职级的权限'),
         array('width'=>10,'name'=>'用工性质'),
         array('width'=>5,'name'=>'职称'),
         array('width'=>15,'name'=>'职称授予时间'),
         array('width'=>10,'name'=>'入党时间'),
         array('width'=>15,'name'=>'发展党员年份'),
         array('width'=>15,'name'=>'党员转正时间'),
         array('width'=>15,'name'=>'入党转正情况'),
         array('width'=>15,'name'=>'发展党员单位'),
         array('width'=>10,'name'=>'党内职务'),
         array('width'=>10,'name'=>'一线情况'),
        );
        $result = array();
        $key = -1;
        $start = time();
        foreach ($data as $val) {
            $bu_name = $val['bu_name'];
            if($val['bu_name'] == '总部' ){
                $val['bu_name'] = '集团';
            }

            $key+=1;
            $temp = array(
            'A'.($key+2)=>'集团/'.$val['company_name'].'/'.$val['depart_city_name'].'/'.$bu_name.$val['department_name'],
            'B'.($key+2)=>$val['position_name'],
            'C'.($key+2)=>$val['user_name'],
            'D'.($key+2)=>'',
            'E'.($key+2)=>($val['identity_type']==1?$val['identity_code']:''),//身份证号码
            'F'.($key+2)=>($val['identity_type']==2?$val['identity_code']:''),//护照号码
            'G'.($key+2)=>($val['identity_type']==1?'身份证':'护照'),//主身份标识
            'H'.($key+2)=>$val['identity_code'].' ',//用户名==身份证号
            'I'.($key+2)=>($val['sex']==1?'男性':'女性'),//性别
            'J'.($key+2)=>(intval($val['last_date'])<=intval(date('Ymd'))?'在职':'离职'),//员工状态
            'K'.($key+2)=>$val['user_type_name'],//员工类别
            'L'.($key+2)=>$val['enter_date'],//入司日期
            'M'.($key+2)=>$val['enter_date'],//担任本职位日期
            'N'.($key+2)=>$val['trial_keep'],//试用期
            'O'.($key+2)=>'',
            'P'.($key+2)=>'',
            'Q'.($key+2)=>$val['office_mail'],//Email地址
            'R'.($key+2)=>'新入职',//入职类型
            'S'.($key+2)=>$val['user_code'],//职员代码
            'T'.($key+2)=>$val['birthday'],//出生日期
            'U'.($key+2)=>'',
            'V'.($key+2)=>'',
            'W'.($key+2)=>'',
            'X'.($key+2)=>$val['native_place'],//出生地
            'Y'.($key+2)=>'',
            'Z'.($key+2)=>'',
            'AA'.($key+2)=>($val['marital_status']==1?'未婚':'已婚'),//婚姻状况
            'AB'.($key+2)=>$val['native_place'],//籍贯
            'AC'.($key+2)=>'',
            'AD'.($key+2)=>'',
            'AE'.($key+2)=>'',
            'AF'.($key+2)=>$val['present_address'],//联系地址
            'AG'.($key+2)=>$val['cellphone'],//联系电话
            'AH'.($key+2)=>'',
            'AI'.($key+2)=>'',
            'AJ'.($key+2)=>$val['identity_address'],//身份证地址
            'AK'.($key+2)=>'',
            'AL'.($key+2)=>$val['cellphone'],//手机号码
            'AM'.($key+2)=>'',
            'AN'.($key+2)=>$val['degree_name'],//现从事
            'AO'.($key+2)=>'',
            'AP'.($key+2)=>$val['bank_name'],//银行名称
            'AQ'.($key+2)=>$val['bank_card'],//银行账号
            'AR'.($key+2)=>'',
            'AS'.($key+2)=>'',
            'AT'.($key+2)=>'',
            'AU'.($key+2)=>'',
            'AV'.($key+2)=>'',
            'AW'.($key+2)=>$val['bu_name'] == '集团' || !$val['bu_name']?$val['bu_name']:$val['bu_name'].'事业部',//成本中心
            'AX'.($key+2)=>'',
            'AY'.($key+2)=>'',
            'AZ'.($key+2)=>'',
            'BA'.($key+2)=>$val['present_address'],//现居住地址
            'BB'.($key+2)=>$val['identity_address'],//户口所在地
            'BC'.($key+2)=>(strpos('test'.$val['company_name'], '瑞庭')?'瑞庭':'瑞家'),//人事档案所在地
            'BD'.($key+2)=>'',
            'BE'.($key+2)=>'',
            'BF'.($key+2)=>'',
            'BG'.($key+2)=>'',
            'BH'.($key+2)=>$val['office_phone'].''.$val['office_phone_ex'],//公司电话
            'BI'.($key+2)=>'',
            'BJ'.($key+2)=>$val['english_name'],//英文名
            'BK'.($key+2)=>'',
            'BL'.($key+2)=>$val['enter_date'],//加入本部门日期
            'BM'.($key+2)=>$val['enter_date'],//加入集团日期
            'BN'.($key+2)=>'',
            'BO'.($key+2)=>$val['enter_date'],//再次入集团日期
            'BP'.($key+2)=>'',
            'BQ'.($key+2)=>$val['indue_date'],//转正日期
            'BR'.($key+2)=>$val['p_m_level'],//职级
            'BS'.($key+2)=>'',
            'BT'.($key+2)=>'',
            'BU'.($key+2)=>'',
            'BV'.($key+2)=>$val['enter_date'],//开始担任当前职务日期
            'BW'.($key+2)=>'',
            'BX'.($key+2)=>'',
            'BY'.($key+2)=>'',
            'BZ'.($key+2)=>'',
            'CA'.($key+2)=>'',
            'CB'.($key+2)=>'',
            'CC'.($key+2)=>'',
            'CD'.($key+2)=>'',
            'CE'.($key+2)=>'',
            'CF'.($key+2)=>'',
            'CG'.($key+2)=>'',
            'CH'.($key+2)=>'',
            'CI'.($key+2)=>'',
            'CJ'.($key+2)=>'',
            'CK'.($key+2)=>'');
            $result[] = $temp;
        }
        $height = array();
        $count = count($data)+1;
        for($i=1;$i<=$count;$i++){
            $height[$i] = 20;
        }
        unset($data);
        //$this->_build_xls($filename,$dl_data);
        PhpExcelHelper::save_excel($filename ,$result,$title_width,$height);
    }
    /**
     * 离职用户导出数据到excel
     * @param array $data
     * @param string $start
     * @param string $end
     * @return string
     */
    public function build_dimission_user($data, $start, $end){
        if(empty($data)) {
            return '';
        }
        $filename = '离职员工_'.$start.'-'.$end.'.xls';
        $dl_index = '<tr height="30">';
        $dl_index .= '<th width="20" bgColor="#CCCCCC" align="center">&nbsp;</th>';
        $dl_index .= '<th width="60" bgColor="#CCCCCC" align="center">姓名</th>';
        $dl_index .= '<th width="60" bgColor="#CCCCCC" align="center">工号</th>';
        $dl_index .= '<th width="40" bgColor="#CCCCCC" align="center">区域</th>';
        $dl_index .= '<th width="60" bgColor="#CCCCCC" align="center">分类</th>';//事业部
        $dl_index .= '<th width="150" bgColor="#CCCCCC" align="center">部门</th>';
        $dl_index .= '<th width="150" bgColor="#CCCCCC" align="center">职位</th>';
        $dl_index .= '<th width="80" bgColor="#CCCCCC" align="center">入职日期</th>';
        $dl_index .= '<th width="80" bgColor="#CCCCCC" align="center">最后工作日</th>';
        $dl_index .= '<th width="80" bgColor="#CCCCCC" align="center">离职日期</th>';
        $dl_index .= '<th width="70" bgColor="#CCCCCC" align="center">在职时长</th>';
        $dl_index .= '<th width="80" bgColor="#CCCCCC" align="center">离职类别</th>';
        $dl_index .= '<th width="150" bgColor="#CCCCCC" align="center">离职原因</th>';
        $dl_index .= '<th width="40" bgColor="#CCCCCC" align="center">备注</th>';
        $dl_index .= '<th width="150" bgColor="#CCCCCC" align="center">邮箱</th>';
        $dl_index .= '</tr>';
        //$dl_list ='';
        $i=1;
        foreach ($data as $key => $val) {
            $t_start= date('m/d/Y',strtotime($val['enter_date']));
            $last_date= date('m/d/Y',strtotime($val['last_date']));
            $l_end =  date('m/d/Y',strtotime($val['leaving_date']));
            $months = $this->_get_sum_month($t_start,$last_date);
            $dl_list .= '<tr height="26">';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$i.'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$val['user_name'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$val['user_code'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$val['city_name'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$val['bu_name'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$val['dep_info']['department_name'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$val['job_info']['position_name'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$t_start.'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$last_date.'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$l_end.'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$months.'</td>';//在职时长
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$val['leave_type_name'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$val['reason_info'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$val['reason_note'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$val['office_mail'].'</td>';
            $dl_list .= '</tr>';
            $i++;
        }
        $dl_data = $dl_index.$dl_list;
        $this->_build_xls($filename,$dl_data);
    }

    private function _build_xls($filename,$content){
        $xls = <<<EOT
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
<table border="1" width="100000">
$content
</table>
</body>
</html>
EOT;
        ob_clean();
        $filename = strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !==false ? urlencode($filename) : $filename;
        header("Content-type:   application/vnd.ms-excel");
        header("Content-Transfer-Encoding: binary");
        header("Accept-Ranges:   bytes");
        header("Accept-Length:   ".strlen($xls));
        header("Content-Disposition:   attachment;   filename=" . $filename);
        echo $xls;
        exit;
    }
    /**
     *
     * @param int $start 20121101
     * @param int $end 20121201
     */
    private function _get_sum_month($start,$end){
        $result = 0;
        $start = date('Ymd',strtotime($start));
        $end = date('Ymd',strtotime($end));
        if($end < $start){
            return $result;
        }
        if(substr($start,0,6)==substr($end,0,6)){
            return 1;
        }
        $end=strtotime($end);
        $start=strtotime($start);
        $timediff = $end - $start;
        $months   = intval($timediff/(86400*30));
        return $months;
    }
}